<?php
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;

class DefaultDb_Repositories_LogRouteToControllerRepository extends EntityRepository
{
    /**
     * Insertamos un registro de asociar una ruta a un controlador
     * @param DefaultDb_Entities_Route $route
     * @param DefaultDb_Entities_User $controller
     * @param DefaultDb_Entities_User $user
     * @return \DefaultDb_Entities_LogRouteToController
     */
    public function add($route, $controller, $user)
    {
        $newLog = new DefaultDb_Entities_LogRouteToController();
        $newLog->setRoute($route);
        $newLog->setController($controller);
        $newLog->setUser($user);
        $em = $this->getEntityManager();
        $em->persist($newLog);
        $em->flush();
        return $newLog;
    }
    
    /**
     * Obtenemos el controlador activo de una ruta.
     * @param integer $routeId Id de la ruta para la cual buscamos el controlador activo
     * @return DefaultDb_Entities_LogRouteToController
     */
    public function getOperationControllerByRoute($routeId)
    {
        $criteria = array('route' => $routeId);
        $orderBy = array('creationDate'=>'desc');
        $logs = $this->findBy($criteria, $orderBy, 1);
        return count($logs) > 0 ? $logs[0] : null;
    }
}